package com.example.backend.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.HashMap;
import java.util.Map;

@JsonInclude
public enum ReturnNo {

    /*****
     * 系统返回码
     */
    OK(0, "操作成功"),
    ERROR(1, "操作失败"),
    AUTH_NO_RIGHT(3, "无权限"),
    RESOURCE_ID_NOT_EXIST(4,"%s对象(id=%d)不存在"),
    STATE_NOT_ALLOW(7,"%s对象（id=%d）%s状态禁止此操作"),

    /****
     * 用户api错误
     */
    USER_ALREADY_EXIT(100, "注册用户名已存在"),
    USER_NOT_EXIT(101, "登陆用户不存在"),
    USER_PASSWORD_ERROR(102, "登陆密码错误");

    /*****
     * 传感器api错误
     */

    @JsonProperty(value = "errcode")
    private int errCode;
    @JsonProperty(value = "errmsg")
    private String errMsg;

    ReturnNo(int errCode, String errMsg) {
        this.errCode = errCode;
        this.errMsg = errMsg;
    }
    public int getErrCode() {
        return errCode;
    }
    public String getErrMsg() {
        return errMsg;
    }

    private static final Map<Integer, ReturnNo> returnNoMap = new HashMap() {
        {
            for (Object enum1 :values()) {
                put(((ReturnNo) enum1).errCode, enum1);
            }
        }
    };
    public static ReturnNo getByCode(int code) {
        ReturnNo[] all = ReturnNo.values();
        for (ReturnNo returnNo : all) {
            if(returnNo.errCode == code) {
                return returnNo;
            }
        }
        return null;
    }
    public static ReturnNo returnReturnNoBuCode(int code) {
        return returnNoMap.get(code);
    }
}
